<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-07-21
 * Time: 15:30
 */

namespace app\applet\controller;


use app\applet\BaseController;

class ActivityController extends BaseController
{
    public function listsAction()
    {
        try {
            $params['where'] = [
                [
                    'categoryId', '=', $this->get['categoryId'],
                ],
                [
                    'status', '=', 1
                ]
            ];

            $params['order'] = ['sort' => 'desc'];

            $data['lists'] = [];
            $activities = $this->app->activityService->getLists($params, $this->page, $this->pagesize);
            if (count($activities)) {
                foreach ($activities as $activity) {
                    $data['lists'][] = [
                        'activityId' => $activity->activityId,
                        'categoryId' => $activity->categoryId,
                        'activityName' => $activity->activityName,
                        'activityIntro' => $activity->activityIntro,
                        'image' => $activity->image,
                        'url' => $activity->url,
                    ];
                }
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($activities);

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);

        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    public function detailAction($id)
    {
        try {
            $activity = $this->app->activityService->getDetail($id);
            $activity->clickCount += 1;
            $activity->save();
            $data = $activity->toArray();
            $data['isUserSignup'] = $this->user?$this->app->activityService->checkSignup($id,$this->user->userId):0;

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);

        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
}